<template>
  <div class="bottom-bar">
    <div>
      <div class="all">
        <check-button :is-checked="isSelectAll" @click.native="allClick"/>
      </div>
      <div class="allText">全选</div>
      <div class="count">合计：¥ {{totalPrice}}</div>
    </div>
    <div class="bar-right">
      <div class="bar-button" @click="calClick">去计算({{checkedLength}})</div>
    </div>
  </div>
</template>

<script>
  import CheckButton from "components/content/checkButton/CheckButton"

  import {mapGetters} from 'vuex'

  export default {
    name: "CartBottomBar",
    components: {
      CheckButton
    },
    computed: {
      ...mapGetters(['cartList']),
      totalPrice() {
        return this.cartList.filter(pro => {
          return pro.checked
        }).reduce((preVal, cur) => {
          return preVal + cur.price * cur.count
        }, 0).toFixed(2)
      },
      checkedLength() {
        return this.cartList.filter(pro => pro.checked).length
      },
      isSelectAll() {
        if(this.cartList.length === 0) return false
        return !this.cartList.find(pro => pro.checked === false)
      }
    },
    methods: {
      allClick() {
        if(this.isSelectAll){
          this.cartList.forEach(item => item.checked = false)
        }else{
          this.cartList.forEach(item => item.checked = true)
        }
      },
      calClick() {
        if(!this.cartList.filter(pro => pro.checked).length){
          this.$toast.show('请选择要购买的商品')
        }
      }
    }
  }
</script>

<style scoped>
.bottom-bar{
  height: 40px;
  background: #eee;
  display: flex;
  font-size: 14px;
  justify-content: space-around;
}
.bottom-bar>div{
  flex: 2;
  display: flex;
}
.all{
  width: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 5px 0 10px;
  font-size: 0;
}
.allText, .count{
  line-height: 40px;
}
.count{
  margin-left: 20px;
}
.bottom-bar .bar-right{
  line-height: 40px;
  flex-direction: row-reverse;
  flex: 1;
}
.bar-button{
  width: 90%;
  background: var(--color-tint);
  color: #fff;
  text-align: center;
}
</style>